#!/bin/bash

# BUGS
# if in the selection two files have the same basename, the last will overwrite the others

# Configuration
BASE_LISTER_URL="http://127.0.0.1/~jerome/svn/phplister3/add.php?plugin=zenityupload"
BASE_LISTER_PHP="public_html/svn/phplister3/"
TMP_DIR="cache/"

# FTP configuration
FTP_SERVER="127.0.0.1"
FTP_USER="jerome"
FTP_PASS="$(cat ../password)"

# Do not modify
# FTP initialisation
FTP_DIR="${BASE_LISTER_PHP}${TMP_DIR}"
FTP="open ${FTP_SERVER}\n"
FTP="${FTP}user ${FTP_USER} ${FTP_PASS}\n" #hash\n"
#FTP="${FTP}cd ${FTP_DIR}\nls\n"

GET=$BASE_LISTER_URL

FILES=$(zenity --file-selection --multiple)"|"

C=1

while [ "$(echo $FILES | cut -d '|' -f$C)" != "" ]
do
	FILE="$(echo $FILES | cut -d '|' -f$C)"
	C=$(($C+1))
	if [ -f "${FILE}" ]
	then
		BASENAME=$(basename "${FILE}")
		FTP="${FTP}put \"${FILE}\" \"${FTP_DIR}${BASENAME}\"\n"
		FTP="${FTP}chmod 0666 \"${FTP_DIR}${BASENAME}\"\n"
		GET="${GET}&data[]=${TMP_DIR}${BASENAME}:${FILE}"
	fi
done

echo "Uploading..."
# it should be easy to improve the progress dialog
echo -e $FTP | ftp -n -i #| zenity --progress --auto-close --pulsate --text="Uploading..." --title="Uploading..." 
echo $(wget "$GET" -q -O - | tail -n1)

exit 0

